﻿using CarRental.DDL;
using CarRental.Helpers;
using CarRental.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace CarRental.View.Order {
    public partial class ConfirmOrder : System.Web.UI.Page {
        DbAccess __dbAccess = new DbAccess();
        car __car = new car();
        string __carId = "";
        string __fromDate = "";
        string __toDate = "";
        DateTime _FromDate = DateTime.Now;
        DateTime _ToDate = DateTime.Now;

        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack) {
                __carId = Request.QueryString["CarId"];
                __fromDate = Request.QueryString["FromDate"];
                __toDate = Request.QueryString["ToDate"];

                _FromDate = DateTime.ParseExact(__fromDate, "yyyy-MM-dd", null);
                _ToDate = DateTime.ParseExact(__toDate, "yyyy-MM-dd", null);

                DateTime _nowDate = DateTime.Now;
                string format = "yyyy-MM-dd";

                //ddlShop.DataSource = new CarRental.Helpers.OptionItems.ShopLst(false);
                //ddlShop.DataValueField = "Key";
                //ddlShop.DataTextField = "Title";
                //ddlShop.DataBind();

                string _orderNo = __dbAccess.GetSeqNo("order");
                txt_orderno.Text = _orderNo;
                txt_orderDate.Text = _nowDate.ToString(format);
                txt_PickkUpDate.Text = __fromDate;
                txt_ReturnDate.Text = __toDate;

                __car = __dbAccess.GetCar().Where(w => w.car_no == __carId).FirstOrDefault();
                if (__car != null) {
                    InitialRecord();
                }
            }
        }

        private void InitialRecord() {
            double _Days = (_ToDate - _FromDate).TotalDays;
            int _totaldays = (int)Math.Ceiling(_Days) + 1;

            txt_CarNo.Text = __car.car_no;
            tbTotalDays.Text = _totaldays.ToString();
            txt_Price.Text = (__car.rent_price).ToString();
            txt_Discount.Text = CalDiscount(_totaldays);
            //ddlShop.SelectedValue = __car.shopcode;
            lblTotalAmount.Text = ((_totaldays * decimal.Parse(txt_Price.Text)) * ((100 - decimal.Parse(txt_Discount.Text)) / 100)).ToString();
        }

        private string CalDiscount(int pDays) {
            string _percent = "";
            if (pDays == 1) {
                _percent = "0";
            } else if (pDays > 2 && pDays < 5) {
                _percent = "5";
            } else if (pDays >= 5) {
                _percent = "10";
            }
            return _percent;
        }

        protected void btn_Confirm_Click(object sender, EventArgs e) {
            order_list _orderLst = new order_list();
            _orderLst.order_no = txt_orderno.Text;
            _orderLst.customer_id = Shared.LoginInfo.LoginName.ToString();
            _orderLst.invoice_date = DateTime.Now;
            _orderLst.staff_id = "";
            _orderLst.status = "NC";
            _orderLst.order_price = decimal.Parse(lblTotalAmount.Text);
            _orderLst.discount = decimal.Parse(txt_Discount.Text);
            _orderLst.last_updated_by = "";
            _orderLst.last_update_date_time = DateTime.Now;

            __dbAccess.InsertOrderLst(_orderLst);


            order_detail _orderDtl = new order_detail();
            _orderDtl.order_no = txt_orderno.Text;
            _orderDtl.sort_no = 1;
            _orderDtl.car_no = txt_CarNo.Text;
            _orderDtl.remarks = txt_Remarks.Text;
            _orderDtl.damage = "N";
            _orderDtl.start_rent_date = DateTime.ParseExact(txt_PickkUpDate.Text, "yyyy-MM-dd", null);
            _orderDtl.prefer_return_date = DateTime.ParseExact(txt_ReturnDate.Text, "yyyy-MM-dd", null);
            _orderDtl.additional_fee = 0;

            __dbAccess.InsertOrderDtl(_orderDtl);
            MessageBox.Show("Make order success", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Response.Redirect("~/View/Order/MakeOrder.aspx");
        }

        protected void btn_Cancel_Click(object sender, EventArgs e) {
            Response.Redirect("~/View/Order/MakeOrder.aspx");
        }
    }
}